<?php

namespace Addons\Category\Model;

use Think\Model;

/**
 * Class AddonCategory
 * @package Addons\AddonCategory\Model
 * @author:温作权
 */
class AddonCategoryModel extends \BaseModel
{

    /*****添加**/
    public $cateList = array();
    public $PcateList = array();
    public $GetparentArray = array();

    /**根据一个分类返回根分类*/
    public function GetRootCate($where)
    {
        $data = $this->where($where)->find();
        while ($data['parent_id']) $data = $this->where(array('cate_id' => $data['parent_id']))->find();
        return $data;
    }

    /**********上一级分类缓存起来***********************/
    function Getparents($cate_id)
    {
        // if(!cacheGet("GetparentArea_".$cate_id)){
        $this->Getparent($cate_id);
        //  cacheAdd("GetparentArea_".$cate_id, $this->GetparentArray, 360000);
        ksort($this->GetparentArray);
        return $this->GetparentArray;
        /*}else{
            return   cacheGet("GetparentArea_".$cate_id);
        } */
    }


    /**********上一级分类***********************/
    function Getparent($cate_id)
    {
        $data = $this->where("cate_id='$cate_id' and is_show=1")->field("cate_id,parent_id,cate_name")->find();

        if ($data) $this->GetparentArray[$data['cate_id']] = $data;
        if (($parent_id = $data['parent_id']) > 0) {
            $data1 = $this->where("cate_id='$parent_id' and is_show=1")->field("cate_id,parent_id,cate_name")->find();
            if ($data1) $this->GetparentArray[$data1['cate_id']] = $data1;
            $this->Getparent($parent_id);
        }


    }

    /**********上一级分类缓存起来***********************/
    function GetparentArea($cate_id)
    {
        // if(!cacheGet("GetparentArea_".$cate_id)){
        $this->Getparent($cate_id);
        //  cacheAdd("GetparentArea_".$cate_id, $this->GetparentArray, 360000);
        ksort($this->GetparentArray);
        return $this->GetparentArray;
        /*}else{
            return   cacheGet("GetparentArea_".$cate_id);
        } */
    }


    //分类数据
    public function GetCateData($store_id = 1, $type)
    {
        $where = "parent_id=0 and  is_show=1 and store_id='$store_id'";
        if ($type) {
            $where .= " and type='$type'";
        }
        $data = $this->where($where)->order("order_by")->select();
        $res = array();
        foreach ($data as $key => $one) {//使用分类ID做键值
            $res[$one['cate_id']] = $one;
            $res[$one['cate_id']]['level'] = 1;
            $res[$one['cate_id']]['subCate'] = $this->sun_catalog($one['cate_id'], 1);
        }
        // dump($res);exit();
        return $res;
    }

    public function cateList($type, $pid)
    {
        $data = $this->where("parent_id='$pid' and type='$type'")->order("order_by")->select();
        //dump($data);
        //dump("store_id='$store_id' and pid='$pid' and type='$type'");
        return $data;
    }

    //递归查询下一级分类
    public function sun_catalog($pid, $level = 0)
    {
        if ($this->where("parent_id='$pid'")->find()) {
            $data = $this->where("parent_id=$pid")->order("order_by")->select();
            $res = array();
            $level++;
            foreach ($data as $key => $one) {
                $res[$one['cate_id']] = $one;
                $res[$one['cate_id']]['level'] = $level;
                $res[$one['cate_id']]['subCate'] = $this->sun_catalog($one['cate_id'], $level);
            }
            return $res;
        }
    }

    /*****返回下一级cate_ids****/
    public function cata_ids($pid)
    {
        $one1 = $this->sun_catalog($pid);
        $this->cata_ids1($one1);
        $this->ids .= $pid;
        return trim($this->ids, ",");
    }

    public function cata_ids1($one1)
    {
        foreach ($one1 as $one) {
            $this->ids .= $one['cate_id'] . ",";
            if ($one['subCate']) $this->cata_ids1($one['subCate']);
        }

    }

    //递归查询上一级分类
    public function p_catalog1($cate_id)
    {
        $one1 = $this->where("cate_id='$cate_id'")->find();
        $this->PcateList[] = $one1;
        if ($cate_id = $one1['parent_id']) {
            $this->p_catalog1($cate_id);
        }
    }

    /*****返回上一级分类数组****/
    public function p_catalog($cate_id)
    {
        $this->PcateList = array();
        $this->p_catalog1($cate_id);
        $data1 = array();;
        for ($i = count($this->PcateList) - 1; $i > -1; $i--) $data1[] = $this->PcateList[$i];
        return $data1;
    }


    /***********对数组分类排列************/
    public function GetCateList($data)
    {
        $p = array();
        foreach ($data as $one) {
            $subcate = $one['subCate'];
            unset($one['subCate']);
            if ($one['level'] > 1) {
                $str = "";
                for ($i = $one['level']; $i > 1; $i--) $str .= "&nbsp;&nbsp;&nbsp;&nbsp;";
                $str1 = $str . "├─";
            }
            $one['name'] = $str1 . $one['cate_name'];
            $this->cateList[] = $one;
            if ($subcate) {
                $this->GetCateList($subcate);
            }
        }

    }


    /***************返回分类信息***********/
    function getCate($store_id = 1, $type)
    {
        $data = $this->GetCateData($store_id, $type);
        $this->GetCateList($data);
        return $this->cateList;
    }


}
